﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using System.IO;
using System.Security.AccessControl;
using System.Configuration;
using System.Reflection;
using Utils;

namespace AlertSystem.Business
{
    public class AppSetting
    {

        public static string ConnectionString
        {
            get { return System.Configuration.ConfigurationManager.ConnectionStrings["MSSQLConnectionString"].ConnectionString; }
            set
            {
                var config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
                var connectionStringsSection = (ConnectionStringsSection)config.GetSection("connectionStrings");
                connectionStringsSection.ConnectionStrings["MSSQLConnectionString"].ConnectionString = value;
                config.Save();
                ConfigurationManager.RefreshSection("connectionStrings");
                _SqlConnectionStringBuilder = new SqlConnectionStringBuilder(value);
            }
        }
        public static SqlConnection GetConnection()
        {
            try
            {
                SqlConnection conn = new SqlConnection(ConnectionString);
                conn.Open();
                return conn;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        static SqlConnectionStringBuilder _SqlConnectionStringBuilder;
        public static SqlConnectionStringBuilder ConnectionStringBuilder
        {
            get
            {
                if (_SqlConnectionStringBuilder == null) _SqlConnectionStringBuilder = new SqlConnectionStringBuilder(ConnectionString);
                return _SqlConnectionStringBuilder;
            }
        }

        public static string DataSource
        {
            get
            {
                return ConnectionStringBuilder.DataSource;
            }
            set
            {
                ConnectionStringBuilder.DataSource = value;
                ConnectionString = ConnectionStringBuilder.ConnectionString;
            }
        }

        public static string Database
        {
            get
            {
                return ConnectionStringBuilder.InitialCatalog;
            }
            set
            {
                ConnectionStringBuilder.InitialCatalog = value;
                ConnectionString = ConnectionStringBuilder.ConnectionString;
            }
        }

        public static string UserID
        {
            get
            {
                return ConnectionStringBuilder.UserID;
            }
            set
            {
                ConnectionStringBuilder.UserID = value;
                ConnectionString = ConnectionStringBuilder.ConnectionString;
            }
        }

        public static string Password
        {
            get
            {
                return ConnectionStringBuilder.Password;
            }
            set
            {
                ConnectionStringBuilder.Password = value;
                ConnectionString = ConnectionStringBuilder.ConnectionString;
            }
        }

        public static string AppModulesPath
        {
            get
            {
                string appPath = Path.GetDirectoryName(typeof(AppSetting).Assembly.Location);
                string modulPath = System.Configuration.ConfigurationManager.AppSettings["App_Modules_Path"];
                if (modulPath != null)
                {
                    string appModulePath = Path.Combine(appPath, modulPath);

                    if (!Directory.Exists(appModulePath))
                    {
                        Directory.CreateDirectory(appModulePath);
                    }
                    return appModulePath;
                }
                return "";
            }
        }

        public static string AppBackupPath
        {
            get
            {
                string appPath = Path.GetDirectoryName(typeof(AppSetting).Assembly.Location);
                string backupPath = System.Configuration.ConfigurationManager.AppSettings["App_Backup_Path"];
                string appbackupPath = Path.Combine(appPath, backupPath);

                if (!Directory.Exists(appbackupPath))
                {
                    Directory.CreateDirectory(appbackupPath);
                }
                return appbackupPath;
            }
        }

        public static string LogBackupFileName
        {
            get
            {
                return Path.Combine(AppBackupPath, System.Configuration.ConfigurationManager.AppSettings["LogBackupFileName"]);
            }
        }

        public static int ConnectionTimeout
        {
            get
            {
                return Convert.ToInt32(System.Configuration.ConfigurationManager.AppSettings["ConnectionTimeout"]);
            }
        }

        public static int ServiceTimeOut
        {
            get
            {
                return Convert.ToInt32(System.Configuration.ConfigurationManager.AppSettings["ServiceTimeOut"]);
            }
        }

        public static int BatchSize
        {
            get
            {
                return Convert.ToInt32(System.Configuration.ConfigurationManager.AppSettings["BatchSize"]);
            }
        }

        public static int MaxQueueElement
        {
            get
            {
                return Convert.ToInt32(System.Configuration.ConfigurationManager.AppSettings["MaxQueueElement"]);
            }
        }

        public static string EmsMethod
        {
            get
            {
                return GetAppSetting("EmsMethod");
            }
        }

        public static string SkypeMethod
        {
            get
            {
                return GetAppSetting("SkypeMethod");
            }
        }

        public static string EmailMethod
        {
            get
            {
                return GetAppSetting("EmailMethod");
            }
        }

        public static string SmtpServer
        {
            get
            {
                return GetAppSetting("SmtpServer");
            }
        }

        public static int SmtpPort
        {
            get
            {
                return GetAppSetting("SmtpPort").ToInt();
            }
        }

        public static string EmailAddress
        {
            get
            {
                return GetAppSetting("EmailAddress");
            }
        }

        public static string EmailPassword
        {
            get
            {
                return GetAppSetting("EmailPassword");
            }
        }

        public static string EmailSubject
        {
            get
            {
                return GetAppSetting("EmailSubject");
            }
        }

        public static string GetAppSetting(string key)
        {
            return System.Configuration.ConfigurationManager.AppSettings[key] ?? "";
        }

        public struct Status
        {
            public const string SENTGATEWAY = "SENTGATEWAY";
            public const string SENTPARTNER = "SENTPARTNER";
            public const string SENT = "SENT";
            public const string PENDING = "PENDING";
            public const string TIMEOUT = "TIMEOUT";
            public const string FAIL = "FAIL";
        }
    }
}
